Fix DSC_RESOURCE_PATH and add DSC_RESTRICTED_PATH#1636
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #1632 by separating “manifest discovery” from “executable discovery” behavior when environment variables are set, introducing DSC_RESTRICTED_PATH (restrict both) and adjusting DSC_RESOURCE_PATH (restrict manifests only).
Changes:
- Add
DSC_RESTRICTED_PATHand give it precedence overDSC_RESOURCE_PATH. - Update command discovery so
DSC_RESOURCE_PATHno longer overwritesPATH(preventing extension executable lookup regressions). - Update/extend Pester tests to reflect the new environment-variable semantics.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/dsc-lib/src/discovery/command_discovery.rs | Implements DSC_RESTRICTED_PATH precedence and adjusts how PATH is handled during discovery. |
| lib/dsc-lib-jsonschema/.versions.json | Bumps JSON schema patch version metadata to V3_2_3. |
| dsc/tests/dsc_set.tests.ps1 | Updates tests to use DSC_RESTRICTED_PATH where prior behavior depended on PATH replacement. |
| dsc/tests/dsc_resource_manifest.tests.ps1 | Switches manifest-related test isolation to DSC_RESTRICTED_PATH. |
| dsc/tests/dsc_resource_list.tests.ps1 | Updates resource listing tests to use DSC_RESTRICTED_PATH (but currently contains a typo). |
| dsc/tests/dsc_resource_input.tests.ps1 | Updates resource input tests to use DSC_RESTRICTED_PATH. |
| dsc/tests/dsc_resource_deprecated.tests.ps1 | Updates deprecated resource tests to use DSC_RESTRICTED_PATH. |
| dsc/tests/dsc_extension_secret.tests.ps1 | Updates secret/extension tests to use DSC_RESTRICTED_PATH. |
| dsc/tests/dsc_extension_manifest.tests.ps1 | Updates extension manifest tests to use DSC_RESTRICTED_PATH. |
| dsc/tests/dsc_extension_import.tests.ps1 | Updates extension import tests to use DSC_RESTRICTED_PATH. |
| dsc/tests/dsc_extension_discover.tests.ps1 | Updates extension discovery tests to use DSC_RESTRICTED_PATH. |
| dsc/tests/dsc_discovery.tests.ps1 | Adds new coverage for restricted vs resource path semantics (but currently has non-portable path construction). |
| dsc/tests/dsc_args.tests.ps1 | Updates argument tests to use DSC_RESTRICTED_PATH. |
| dsc/tests/dsc_adapter.tests.ps1 | Updates adapter-related tests to use DSC_RESTRICTED_PATH. |
| adapters/powershell/Tests/win_powershellgroup.tests.ps1 | Updates Windows PowerShell adapter tests to use DSC_RESTRICTED_PATH. |
| adapters/powershell/Tests/win_powershell_cache.tests.ps1 | Updates Windows PowerShell adapter cache tests to use DSC_RESTRICTED_PATH. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
lib/dsc-lib/src/discovery/command_discovery.rs:180
DSC_RESOURCE_PATHbeing set currently disables PATH mutation even whenallow_env_overrideis false (the env var is ignored for discovery, but it still changes behavior). Gate this check onallow_env_overrideso PATH behavior is unchanged unlessDSC_RESOURCE_PATHis actually being honored.
} else if dsc_resource_path.is_none() { // if DSC_RESOURCE_PATH is used, we don't want to modify the PATH env var, as it is intended to be used for resource discovery only
I think this might be a valid comment |
I believe |
I see. Is the |
|
@tgauth thinking about this further and to reduce complexity in the code and also for users to think through, I'm going to simply define
I think the current PR covers this behavior. However, there's one change I need to make. Even if |
4491f09 to
b24d1a7
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 16 out of 16 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
lib/dsc-lib/src/discovery/command_discovery.rs:184
DSC_RESOURCE_PATHbeing set influences PATH/resource-path handling even whenallowEnvOverrideis false. In that case, env override is ignored for discovery, but this branch still runs and preventsadd_exe_home_to_path(paths)from adding the DSC exe home to the manifest search paths (and may still mutate PATH). Gate this branch onallow_env_overrideso env vars are truly ignored when policy disables them.
} else if dsc_resource_path.is_some() {
// just add exe home to PATH env var if not already in PATH env var
let env_paths = env::var_os("PATH").map(|paths| env::split_paths(&paths).collect::<Vec<_>>()).unwrap_or_default();
_ = add_exe_home_to_path(env_paths)?;
} else {
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 16 out of 16 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
lib/dsc-lib/src/discovery/command_discovery.rs:186
- When
allowEnvOverrideisfalse,DSC_RESOURCE_PATHis ignored for discovery (theelse { ... }branch buildspathsfrom the configured directories/PATH). However, later theelse if dsc_resource_path.is_some()branch still runs, which meansadd_exe_home_to_pathis applied only to the currentPATHand does not add the exe home intopaths. This makes behavior depend on whetherDSC_RESOURCE_PATHmerely exists (even when override is disabled) and can cause built-in manifests in the exe directory to be skipped when the exe home isn’t already in PATH.
Gate the “DSC_RESOURCE_PATH is set” branch on allow_env_override and on actually using that env var for discovery (similarly to the restricted-path branch), so the presence of the env var can’t change behavior when overrides are disabled.
if resource_path_setting.allow_env_override && dsc_restricted_path.is_some() {
// when using restricted path, intent is to isolate the search of manifests and executables to the restricted path
// so we replace the PATH with the restricted path
if let Ok(new_path) = env::join_paths(paths.clone()) {
unsafe {
env::set_var("PATH", new_path);
}
} else {
return Err(DscError::Operation(t!("discovery.commandDiscovery.failedJoinEnvPath").to_string()));
}
} else if dsc_resource_path.is_some() {
// just add exe home to PATH env var if not already in PATH env var
let env_paths = env::var_os("PATH").map(|paths| env::split_paths(&paths).collect::<Vec<_>>()).unwrap_or_default();
_ = add_exe_home_to_path(env_paths)?;
} else {
// if exe home is not already in PATH env var then add it to env var and list of searched paths
paths = add_exe_home_to_path(paths)?;
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 16 out of 16 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
lib/dsc-lib/src/discovery/command_discovery.rs:183
DSC_RESOURCE_PATHbeing set currently triggers PATH mutation even whenallowEnvOverrideis false. In that case the env var is ignored for manifest discovery (falls back to configured directories/PATH), but thiselse if dsc_resource_path.is_some()branch still callsadd_exe_home_to_path(...)and can change PATH as a side effect. Gate this branch onresource_path_setting.allow_env_overrideso disabling env override fully disables env-var-driven PATH changes.
} else if dsc_resource_path.is_some() {
// just add exe home to PATH env var if not already in PATH env var
let env_paths = env::var_os("PATH").map(|paths| env::split_paths(&paths).collect::<Vec<_>>()).unwrap_or_default();
_ = add_exe_home_to_path(env_paths)?;
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (2)
dsc/tests/dsc_discovery.tests.ps1:387
- This test builds the manifest path using a hard-coded
/separator (e.g..../echo.dsc.resource.json). UseJoin-Pathfor platform-independent paths to avoid failures on hosts where mixed separators are not normalized.
$manifest1 = Get-Content -Raw -Path "$(Split-Path -Path $dscEcho.Source -Parent)/echo.dsc.resource.json" | ConvertFrom-Json -AsHashtable
$manifest1.type = 'Test/RestrictedPathResource1'
$manifest1.condition = "[not(equals(tryWhich('dscecho'), null()))]"
Set-Content -Path (Join-Path $exePath1 'test.dsc.resource.json') -Value ($manifest1 | ConvertTo-Json -Depth 10)
$manifest2 = Get-Content -Raw -Path "$(Split-Path -Path $dscEcho.Source -Parent)/echo.dsc.resource.json" | ConvertFrom-Json -AsHashtable
dsc/tests/dsc_discovery.tests.ps1:413
- These
Copy-Itempaths are constructed via string concatenation with/(e.g."$dscHome/psscript.dsc.resource.json"). PreferJoin-Pathso the test remains path-separator agnostic across Windows/Linux/macOS.
Copy-Item -Path "$dscHome/psscript.dsc.resource.json" -Destination "$testdrive"
Copy-Item -Path "$dscHome/echo.dsc.resource.json" -Destination "$testdrive"
Copy-Item -Path "$dscHome/dscecho*" -Destination "$testdrive"
PR Summary
The behavior is that
allow_env_override(trueby default) restricts whetherDSC_RESTRICTED_PATHorDSC_RESOURCE_PATHgets applied.DSC_RESTRICTED_PATHwhich limits both manifest and exe discovery, this works by replacing the PATH env varDSC_RESOURCE_PATHto be limited only to manifest discovery and not exe discovery by not overwriting PATH, DSC home path is added to PATHDSC_RESOURCE_PATHbehavior, so searched and replaced them to useDSC_RESTRICTED_PATHinsteadDSC_RESTRICTED_PATHtakes precedence ifDSC_RESOURCE_PATHis also definedPR Context
Fix #1632